package com.bigknow.appstore.web.console.controller;

import com.bigknow.appstore.web.base.controller.entity.TreeNode;
import com.bigknow.appstore.web.base.vo.FancyTreeVo;
import com.bigknow.appstore.web.console.app.Application;
import com.bigknow.appstore.web.console.service.AppStatisticsService;
import com.bigknow.appstore.web.console.service.impl.ApplicationCategoryService;
import com.bigknow.net.minidev.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * Created by chaos on 2016/10/12.
 */
@RestController
@RequestMapping("/app/statistics")
public class AppStatisticsController {

    @Autowired
    private AppStatisticsService service;

    @Autowired
    private ApplicationCategoryService applicationCategoryService;

    @RequestMapping("cover")
    public ModelAndView appUserCover() {
        return new ModelAndView("sys/statistics/appStatisticsCover");
    }

    @RequestMapping("sysCover")
    public ModelAndView appSysCover() {
        return new ModelAndView("sys/statistics/sysStatisticsCover");
    }

    /**
     * 应用状态饼图数据
     *
     * @return
     */
    @RequestMapping("rest/appStatePieData")
    public String getAppStatePieData() {
        JSONObject js = new JSONObject();
        js.put("title", service.getAppStatePieTitle());
        js.put("data", service.getAppStatePieData());
        return js.toJSONString();
    }

    /**
     * 打开用户的应用发布情况页面
     *
     * @return
     */
    @RequestMapping("appStatePage")
    public ModelAndView appStatePage() {

        return new ModelAndView("sys/statistics/appStatePie");
    }

    /**
     * 打开用户的应用发布情况页面----子页面appStatePieListPart
     *
     * @param model
     * @param state
     * @return
     */
    @RequestMapping("appStatePieListPart/{state}")
    public ModelAndView appStatePieListPart(Model model, @PathVariable String state) {
        List<Application> apps = service.getAppsByCurrentUser(state);
        for (Application app : apps) {
            app.setCategory(applicationCategoryService.getCategory(app.getCategoryId()));
        }
        model.addAttribute("data", apps);
        return new ModelAndView("sys/statistics/appStatePieListPart", model.asMap());
    }

    /**
     * 应用使用总览页面
     *
     * @return
     */
    @RequestMapping("appOverallCall")
    public ModelAndView appOverallCall() {
        return new ModelAndView("sys/statistics/appOverallCall");
    }

    @RequestMapping("rest/appCalledOverview/{id}")
    public String appCalledOverview(@PathVariable String id) {
        return service.appCalledOverview(id);
    }

    /**
     * 应用总体使用柱状图数据
     *
     * @return
     */
    @RequestMapping("rest/callTimesTotalBarData")
    public String callTimesTotalBarData() {
        return service.callTimesTotalBarData();
    }

    /**
     * 应用树
     *
     * @return
     */
    @RequestMapping("rest/apptree")
    public List<FancyTreeVo> appTree() {
        return service.appTree();
    }

    /**
     * 单个应用调用次数趋势图
     *
     * @param id
     * @return
     */
    @RequestMapping("rest/appCallTimesLineData/{id}")
    public String appCallTimesLineData(@PathVariable String id) {
        return service.appCallTimesLineData(id);
    }

    @RequestMapping("rest/appCallDataSizeLineData/{id}")
    public String appCallDataSizeLineData(@PathVariable String id) {
        return service.appCallDataSizeLineData(id);
    }

}
